﻿using EmprestaAE.MVVM.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmprestaAE.MVVM.ViewModel
{
    class DatePickerVM : ViewModelBase
    {
        #region Singleton
        private static DatePickerVM instance;

        public static DatePickerVM Instance
        {
            get { return instance ?? (instance = new DatePickerVM()); }
        }
        #endregion

        public event Action DatePickeredChanged;

        private DateModel date;

        public DateModel Date
        {
            get { return date ?? (date=new DateModel()); }
            set { if (date != value) { date = value; RaisePropertyChanged("Date"); } }
        }

        private string teste;

        public string Teste
        {
            get { return teste ?? (teste = "oi!"); }
            set { if (teste != value) { teste = value; RaisePropertyChanged("Teste"); } }
        }

        private int lendingTime;

        public int LendingTime
        {
            get { return lendingTime; }
            set { if (lendingTime != value) { lendingTime = value; RaisePropertyChanged("LendingTime"); } }
        }


        public DatePickerVM()
        {
            PropertyChanged += DatePickerVM_PropertyChanged;
        }

        void DatePickerVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "LendingTime":
                    UpdateDateLendingTime();                                        
                    break;                
                default:
                    break;
            }
        }
        private void UpdateDateLendingTime()
        {
            Date = new DateModel() { Date = DateTime.Now.AddDays(lendingTime) };
        }

        private VMCommand increaseLendingTimeCommand;

        public VMCommand IncreaseLendingTimeCommand
        {
            get { return increaseLendingTimeCommand ?? (increaseLendingTimeCommand = new VMCommand(IncreaseLendingTimeCommandAction)); }            
        }

        private void IncreaseLendingTimeCommandAction(object param)
        {
            if ((LendingTime < MAXIMUMLENDINGTIME))
            LendingTime += 1;
        }

        private VMCommand decreaseLendingTimeCommand;

        public VMCommand DecreaseLendingTimeCommand
        {
            get { return decreaseLendingTimeCommand ?? (decreaseLendingTimeCommand = new VMCommand(DecreaseLendingTimeCommandAction)); }            
        }

        private void DecreaseLendingTimeCommandAction(object obj)
        {
            if ((LendingTime > MINIMUMLENDINGTIME))
            LendingTime -= 1;
        }

        private VMCommand pickDateCommand;

        public VMCommand PickDateCommand
        {
            get { return pickDateCommand ?? (pickDateCommand = new VMCommand(PickDateCommandAction)); }            
        }

        private void PickDateCommandAction(object obj)
        {
            DatePickeredChanged();
            NavigationService.GoBackCommand.Execute(null);
        }
        

        private const double MAXIMUMLENDINGTIME = 60;
        public double MaximumLendingTime{ get { return MAXIMUMLENDINGTIME; } }

        public const double MINIMUMLENDINGTIME = 0;
        public double MinimumLendingTime { get { return MINIMUMLENDINGTIME; } }

        private bool datePickered;

        public bool DatePickered
        {
            get { return datePickered; }
            set { if (datePickered != value) { datePickered = value; RaisePropertyChanged("DatePickered"); } }
        }
        
        
    }
}
